define(['app'],function(app){
    app.register.controller('login', function($scope, $http, $state, $cookieStore,$Ajax){
        var http = app._baseUrl;
        //顶部错误提示tip
        $scope.errorShow = {
            isShow:false,
            authError:'',
            hide:function(){
                this.isShow = false;
            },
            show:function(){
                this.isShow = true;
            }
        };
        //失焦
        $scope.blur=function(){
            $scope.errorShow.hide();
        };
        //回车
        $scope.keyupLogin = function (e) {//监听键盘按下事件
            var keycode = window.event ? e.keyCode : e.which;
            if (keycode == 13) {
                $scope.loginPage.click();
            }
        };
        //登录按钮点击事件
        $scope.loginPage = {
            isAjax:true,
            mobile:'',
            password:'',
            requs:function(){
                var url = http + '/Pub/Public/login';
                var data = {
                    phone: this.mobile,
                    password: this.password
                };
                $Ajax.postData(url,data).then(function(res){
                    if(res.data.success){//登录成功
                        var user = {
                            user_id: res.user_id,
                            mobile:res.phone,
                            comp: {
                                id: res.company_id
                            },
                            post: {
                                id: res.post_id
                            }
                        };
                        $cookieStore.put('refurbish',1);//不知道干嘛的先留着吧不知道哪一天就删除了
                        $cookieStore.put('userInfo', JSON.stringify(res.data));
                        $state.go('app.myCourse');//此段代码是在网商云学院v1.0.6版本去掉产品模块时添加
                    }else {//登录请求失败
                        $scope.errorShow.authError = res.data.message;
                        $scope.errorShow.show();
                    }
                })
            },
            click:function(){
                if($scope.mobile==this.mobile&&$scope.password==this.password){
                    this.isAjax = false;
                    $scope.errorShow.show();
                }else{
                    this.isAjax = true;
                    this.mobile = $scope.mobile;
                    this.password = $scope.password;
                }
                if(!this.mobile || !this.password){
                    $scope.errorShow.authError = '用户名密码不能为空';
                    $scope.errorShow.show();
                    return false;
                }else{
                    if(this.isAjax){
                        this.requs();
                    }
//                    var exp=/^1[3|4|5|7|8][0-9]{9}$/;//验证账号正则
//                    if(exp.test(this.mobile)){
//                        if(this.isAjax){
//                            this.requs();
//                        }
//                    }else{
//                        $scope.errorShow.authError = '账号格式输入错误';
//                        $scope.errorShow.show();
//                    }

                }
            }
        };
    })
});